package com.safeluck.scannerqcr.fragment.shop;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SimpleAdapter;

import com.safeluck.scannerqcr.R;
import com.safeluck.scannerqcr.adapter.MenuAdapter;
import com.safeluck.scannerqcr.fragment.BaseMainFragment;
import com.safeluck.scannerqcr.fragment.WechatFourthTabFragment;
import com.safeluck.scannerqcr.listener.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

import me.yokeyword.fragmentation.ISupportFragment;

/**
 * ScannerQCR
 * Created by lzw on 2017/12/5. 11:57:26
 * 邮箱：632393724@qq.com
 * All Rights Saved! Chongqing AnYun Tech co. LTD
 */

public class MenuFragment extends BaseMainFragment {

    private static final String ARG_MENUS = "arg_menus";

    private static final java.lang.String SAVE_STATE_POSITION = "save_state_pos";
    private List<String> mMenus;
    private RecyclerView recy;
    private MenuAdapter myMenuadapter;
    private int mCurrintPos;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.menu_fragment,container,false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        recy = view.findViewById(R.id.recy_menu);
        recy.setLayoutManager(new LinearLayoutManager(_mActivity));
        recy.setHasFixedSize(true);
        myMenuadapter = new MenuAdapter(_mActivity);
        recy.setAdapter(myMenuadapter);
        myMenuadapter.setDatas(mMenus);

        myMenuadapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position, View view, RecyclerView.ViewHolder viewHolder) {
                showContent(position);
            }
        });
    }

    private void showContent(int position) {
        if (mCurrintPos == position){
            return;
        }
        mCurrintPos = position;
        myMenuadapter.setItemChecked(position);
        ((WechatFourthTabFragment)getParentFragment()).switchFragment(((ContentFragment) ContentFragment.newInstance(mMenus.get(position))));
    }

    public static ISupportFragment newInstance(ArrayList<String> listMenus) {
        Bundle bundle = new Bundle();
        bundle.putStringArrayList(ARG_MENUS,listMenus);
        MenuFragment menu = new MenuFragment();
        menu.setArguments(bundle);
        return  menu;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle args = getArguments();
        if (args != null){
            mMenus = args.getStringArrayList(ARG_MENUS);
        }
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (savedInstanceState != null){
            mCurrintPos = savedInstanceState.getInt(SAVE_STATE_POSITION);
            myMenuadapter.setItemChecked(mCurrintPos);
        }else {
            mCurrintPos = 0;
            myMenuadapter.setItemChecked(0);
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(SAVE_STATE_POSITION,mCurrintPos);
    }
}
